package com.bjpowerndoe.ylb.web.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjpowerndoe.ylb.common.AppTools;
import com.bjpowerndoe.ylb.domain.ProductInfoPO;
import com.bjpowerndoe.ylb.web.enums.CodeEnum;
import com.bjpowerndoe.ylb.web.model.CommonResult;
import com.bjpowerndoe.ylb.web.model.PageInfo;
import com.bjpowerndoe.ylb.web.model.dto.ProductDTO;
import com.bjpowerndoe.ylb.web.model.dto.ProductDetailDTO;
import com.bjpowerndoe.ylb.web.model.vo.BidUserMapVO;
import com.bjpowerndoe.ylb.web.model.vo.ProductVO;
import com.bjpowerndoe.ylb.web.service.inter.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "理财产品模块")
@RestController
@RequiredArgsConstructor
public class ProductController {
    private final ProductService productService;


    //获取首页理财产品列表
    @ApiOperation(value = "三类理财产品列表",notes = "项目首页产品列表")
    @GetMapping("/product/index")
    public CommonResult queryIndexProductLIst(){
        IPage<ProductInfoPO> xinResult = productService.queryPageByType(0, 1, 1);
        IPage<ProductInfoPO> youResult = productService.queryPageByType(1, 1, 3);
        IPage<ProductInfoPO> sanResult = productService.queryPageByType(2, 1, 3);
        //创建VO类,创建HashMap
        //同名属性赋值
        List<ProductVO> xinVOList = BeanUtil.copyToList(xinResult.getRecords(), ProductVO.class);
        List<ProductVO> youVOList = BeanUtil.copyToList(youResult.getRecords(), ProductVO.class);
        List<ProductVO> sanVOList = BeanUtil.copyToList(sanResult.getRecords(), ProductVO.class);

        Map<String,Object> data = new HashMap<>();
        data.put("xinList",xinVOList);
        data.put("youList",youVOList);
        data.put("sanList",sanVOList);

        return CommonResult.ok(data);
    }

    //按产品类型查询理财产品
    @GetMapping("/product/type")

    public CommonResult queryType(Integer type,Integer pageNo,Integer pageSize){

        CommonResult commonResult =CommonResult.fail(CodeEnum.REQUEST_PARAM_ERR);
        // 检查请求参数
        if (type == 1 || type == 2) {
            pageNo = AppTools.defaultPageNo(pageNo);
            pageSize = AppTools.defaultPageSize(pageSize);
            IPage<ProductInfoPO> pageResult = productService.queryPageByType(type, pageNo, pageSize);

            // 获取数据
            List<ProductInfoPO> productPOList = pageResult.getRecords();

            // 分页数据
            long pages = pageResult.getPages();
            long size = pageResult.getSize();
            long current = pageResult.getCurrent();
            PageInfo pageInfo = new PageInfo(current,size,pages);

            // 放入CommonResult
            Map<String,Object> data = new HashMap<>();
            data.put("productList",BeanUtil.copyToList(productPOList,ProductVO.class));
            data.put("pageInfo",pageInfo);
            commonResult = CommonResult.ok(data);
        }
 
        return commonResult;
    }

    //产品详情
    @GetMapping("/product/detail")
    public CommonResult queryDetailById(@RequestParam(value = "productId",required = true) Integer productId,
                                        @RequestHeader(value = "uid",required = false) Integer uid){
        CommonResult commonResult = CommonResult.fail(CodeEnum.FAIL);
        if( productId > 0 ){
            ProductDetailDTO productDetailDTO = productService.queryDetailByProductId(productId, uid);
            if(productDetailDTO != null){
                // 获取数据，封装给CommonResult
                Map<String,Object> data = new HashMap<>();
                ProductDTO productDTO = productDetailDTO.getProductDTO();
                data.put("product",BeanUtil.copyProperties(productDTO,ProductVO.class));

                //投资记录
                data.put("bidList",BeanUtil.copyToList(productDetailDTO.getBidList(), BidUserMapVO.class));

                //账号金额
                data.put("accountMoney",productDetailDTO.getAccountMoney());

                commonResult = CommonResult.ok(data);
            }
        }

        return commonResult;
    }
}
